<?php

namespace App\Store\Controllers\Financial\Statements;

use App\Common\Services\CommonExcelExport;
use App\Common\Services\Store\Common as StoreCommon;
use App\Http\Controllers\Controller;
use App\Store\Models\Financial\Statements\Rate;
use Illuminate\Http\Request;
use App\Store\Validate\Financial\Statements\Rate as ValidateRate;
use Maatwebsite\Excel\Facades\Excel;

/**
 * 费率表
 */
class RateController extends Controller
{
    public function dopostadd(Request $request)
    {
        $req_data = $request->only(['store_id', 'pay_type_id', 'money_start', 'money_end', 'val_type', 'regular_value', 'rate_value']);
        $Validate = new ValidateRate();
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $cp_id = $accountinfo['cp_id'];
        $moneylog_data = [
            'cp_id' => $cp_id,
            'store_id' => $req_data['store_id'],
            'pay_type_id' => $req_data['pay_type_id'],
            'money_start' => $req_data['money_start'],
            'money_end' => $req_data['money_end'],
            'val_type' => $req_data['val_type'],
            'regular_value' => !empty($req_data['regular_value']) ? $req_data['regular_value'] : 0,
            'rate_value' => !empty($req_data['rate_value']) ? $req_data['rate_value'] : 0,
            'create_account_id' => $accountinfo['id'],
            'create_account_real_name' => $accountinfo['real_name'],
            'create_at' => time(),
        ];
        Rate::create($moneylog_data);
        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }

    /**
     * 列表
     */
    public function getlist(Request $request)
    {
        $list = Rate::getList($request);
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $list]);
    }

    /**
     * 详情
     */
    public function getone(Request $request)
    {
        $one = Rate::where('id', $request['id'])->first();
        if ($one) {
            $one = $one->toArray();
            return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $one]);
        }
        return response()->json(['code' => '000', 'msg' => '获取失败']);
    }

    /**
     * 编辑
     */
    public function dopostedit(Request $request)
    {
        $req_data = $request->only(['id', 'store_id', 'pay_type_id', 'money_start', 'money_end', 'val_type', 'regular_value', 'rate_value']);
        $Validate = new ValidateRate('edit');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        Rate::where('id', $request['id'])->update($req_data);
        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }

    /**
     * 删除
     */
    public function dopostdel(Request $request)
    {
        $req_data = $request->only(['id']);
        $Validate = new ValidateRate('del');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $req_data['is_del']                   = 2;
        $req_data['update_account_id']        = $accountinfo['id'];
        $req_data['update_account_real_name'] = $accountinfo['real_name'];
        $req_data['update_at']                = time();

        Rate::where('id', $request['id'])->update($req_data);
        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }

    public function doexport(Request $request)
    {
        $list = Rate::getList($request)['list'];
        $exceldata = [];
        $storeNames = array_unique(array_map(function($item) {
            return $item['storeinfo']['store_name'];
        }, $list));
        $row = [

            ['id'=>'费率表'],
            ['id'=>'所属门店：'.join(',' ,$storeNames)],
            [
                'id' => '序号',
                'store_name' => '所属门店',
                'val_type' => '费率类型',
                'paytype_name'=>'支付方式',
                'money_start' => '开始金额',
                'money_end' => '结束金额',
                'regular_value' => '固定值',
                'rate_value' => '费率(%)',
            ]
        ];
        foreach ($list as $key=>$val) {
            $exceldata[] = [
                'id' => $key+1,
                'store_name' => $val['storeinfo']['store_name'],
                'val_type' => $val['val_type']==1?'固定值':'费率',
                'paytype_name'=>$val['pay_type_id'] == -1?'混合支付':(isset($val['paytypeinfo']['paytype_name'])?$val['paytypeinfo']['paytype_name']:''),
                'money_start' => $val['money_start']/100,
                'money_end' => $val['money_end']/100,
                'regular_value' => $val['regular_value']/100,
                'rate_value' => $val['rate_value'],
            ];
        }

        $filename = '费率信息导出_' . time() . '.xlsx';
        return Excel::download(new CommonExcelExport($row, $exceldata), $filename);
    }
}
